//先定义了一个函数 getShieldingWordsList，
//用于从后端获取屏蔽词列表。接着，
//定义了两个辅助函数 containsShieldingWords 和 replaceShieldingWords，
//分别用于判断文本是否包含屏蔽词和将文本中的屏蔽词替换为 *。
// 获取屏蔽词列表

import axios from "axios"

export function getShieldingWordsList() {
    return axios.get('/sensitivewords/list')
        .then(res => res.data)
        .catch(error => {
            console.log(error)
            return []
        })
}

// 判断文本是否包含屏蔽词
export function containsShieldingWords(text, shieldingWords) {
    if (!text || !shieldingWords || shieldingWords.length === 0) {
        return false
    }
    for (let i = 0; i < shieldingWords.length; i++) {
        if (text.includes(shieldingWords[i].word)) {
            return true
        }
    }
    return false
}

// 替换屏蔽词为 *
export function replaceShieldingWords(text, shieldingWords) {
    if (!text || !shieldingWords || shieldingWords.length === 0) {
        return text
    }
    let result = text
    for (let i = 0; i < shieldingWords.length; i++) {
        const word = shieldingWords[i].word
        const reg = new RegExp(word, 'gi')
        result = result.replace(reg, '*'.repeat(word.length))
    }
    return result
}